$(document).ready(function () {

    $("#search_channel").on("input", function (event) {
        searchNode(event.target.value);
    });

    getData();

});

var treeObj;  //ztree 对象
var treeNodeData;

function searchNode(value) {
    updateNodes(null, false);
    if (value.trim().length > 0) {
        var nodeList = treeObj.getNodesByParamFuzzy("name", value) || [];

        if (nodeList && nodeList.length > 0) {
            treeObj.expandAll(false);
            treeObj.expandNode(treeObj.getNodes()[0], true, false, true);
            updateNodes(nodeList, true);
        }
    }

}


function updateNodes(nodeList, highlight) {
    if (!highlight) {
        updateByParent(treeObj.getNodes(), highlight);
    } else {
        for (var i = 0, len = nodeList.length; i < len; i++) {
            nodeList[i].highlight = highlight;
            treeObj.updateNode(nodeList[i]);
            if (highlight) {
                expandNode(nodeList[i], true);
            }

        }
    }
}

function updateByParent(nodeList, highlight) {
    if (nodeList && nodeList.length > 0) {
        for (var i = 0, len = nodeList.length; i < len; i++) {
            var node = nodeList[i];
            node.highlight = highlight;
            treeObj.updateNode(node);
            if (node.children && node.children.length > 0) {
                updateByParent(node.children, highlight);
            }
        }
    }
}

function expandNode(treeNode, isExpend) {
    if (treeNode) {
        var parentNode = treeNode.getParentNode();
        if (parentNode != null) {
            treeObj.expandNode(parentNode, true, false, true);
            expandNode(parentNode, isExpend);
        }
    }

}

function getData() {

    $X('/order/queryService', 'getNotifyTime').callx({
        onResult: function (data) {
            if (data) {
                $('#notifyTime').val(data);
            }
        }
    });

    $X('/boss/channelService', 'listChildrenChannelByTopCode').callx({
        topChannelCode: 'POS',
        onResult: function (data) {
            if (data && data.length > 0) {
                var tree = JSON.stringify(data);
                tree = jQuery.parseJSON(tree);
                initTree(tree);

                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                zTree.checkAllNodes(true);
                $X('/order/queryService', 'listDisableStore').callx({
                    onResult: function (data) {
                        if (data != null) {
                            for (var i = 0; i < data.length; i++) {
                                var node = treeObj.getNodeByParam("channelCode", data[i].storeCode);
                                treeObj.checkNode(node, false, false, false);
                            }
                        }
                    }
                });

            }
        },
        onError: function (error) {
            onError(error);
        }
    });
}

function initTree(tree) {
    //配置树的属性
    var setting = {
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: {"Y": "s", "N": "ps"}
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "channelCode",
                pIdKey: "parentCode"
            },
            key: {
                name: "name"
            }
        },

        view: {
            fontCss: getFontCss
        }
    }
    //创建自定义树
    treeObj = $.fn.zTree.init($("#treeDemo"), setting, tree);
}

function queryData() {
    $("#query-form").modal("show");
    console.log("123");
}

function getFontCss(treeId, treeNode) {
    return (!!treeNode.highlight) ? {color: "#A60000", "font-weight": "bold"} : {
        color: "#333",
        "font-weight": "normal"
    };
}


function saveData() {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    // console.log(zTree);
    var nodes = zTree.getCheckedNodes(true);
    // console.log(nodes);
    var channelCodes = new Array();
    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].hasLeaf == 0 && nodes[i].hasLeaf != '') {
            channelCodes.push(nodes[i].channelCode);
        }

    }
    // console.log(channelCodes);

    var topChannelCodes = $('#topChannelCode').val();

    // console.log(topChannelCodes);

    var topChannelCodeList = topChannelCodes.split(",");

    var topChannelCodeArray = new Array();
    if(topChannelCodeList.length > 0){
        $.each(topChannelCodeList,function (index, val) {
            if(val !== ""){
                topChannelCodeArray.push(val);
            }
        })
    }
    $X('/order/operateService', 'saveVoiceSetting').callx({
        channelCodes: channelCodes,
        notifyTime: $('#notifyTime').val(),
        topChannelCodeArray:topChannelCodeArray,
        // notifyFrequency:$('#notifyFrequency').val(),
        onResult: function (data) {
            if (data) {
                layer.msg('配置保存成功!');
            } else {
                layer.msg('配置保存失败!');
            }
        }
    });
}


